package com.atguigu.gmall.minio.config;

import com.atguigu.gmall.minio.propreties.MinioProperties;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 卡尼奶
 * @Date 2022/5/20 14:33
 * @Version 1.0
 */
@Configuration
public class MinioConfig {

    @Autowired
    private MinioProperties minioProperties;


    /**
     * Minion初始化放入Bean容器中
     * @return
     * @throws Exception
     */
    @Bean
    public MinioClient minioClient() throws Exception {
        MinioClient minioClient = new MinioClient(minioProperties.getEndpoint(),minioProperties.getAccessKey(),minioProperties.getSecretKey());
        // 如存储桶不存在，创建之。
        boolean found = minioClient.bucketExists("gmall");
        if (found) {
            System.out.println("mybucket already exists");
        } else {
            // 创建名为'my-bucketname'的存储桶。
            minioClient.makeBucket("gmall");
            System.out.println("mybucket is created successfully");
        }
        return minioClient;
    }
}
